
package com.eltonkent.ui.adapters;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.eltonkent.R;
import com.eltonkent.ui.ApplicationConstants;
import com.eltonkent.ui.containers.SimpleIconListContainer;
import com.eltonkent.ui.wrappers.TemplateListWrapper;
import com.eltonkent.util.ImageUtil;

public class SimpleIconListAdapter extends ArrayAdapter<SimpleIconListContainer>
{

	private int currentTextSize;

	public SimpleIconListAdapter(SharedPreferences prefs, Context context, int textViewResourceId,
			List<SimpleIconListContainer> objects)
	{
		super(context, textViewResourceId, objects);
		currentTextSize = prefs.getInt(ApplicationConstants.PREF_TEXT_SIZE,
				ApplicationConstants.PREF_DEF_TEXTSIZE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View layout = convertView;
		TemplateListWrapper wrapper;
		if(layout == null)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());

			layout = inflater.inflate(R.layout.listview_icon_singleline_templates, parent, false);
			wrapper = new TemplateListWrapper(layout);
			layout.setTag(wrapper);
		} else
		{
			wrapper = (TemplateListWrapper) layout.getTag();
		}

		SimpleIconListContainer listData = getItem(position);
		wrapper.getIcon().setImageBitmap(
				ImageUtil.createBitmapFromAsset(getContext(), listData.getIconResource()));

		wrapper.getText().setText(listData.getTemplateName());
		wrapper.getText().setTextSize(currentTextSize);
		return layout;
	}
}
